<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  Thyon Design 2008 
 * @author     John Brand <john.brand@thyon.com> 
 * @package    NewsletterExport 
 * @license    LGPL
 * @filesource
 */


/**
 * Class NewsletterExport
 *
 * Provide methods to handle subscriber export.
 * @copyright  Thyon Design 2008 
 * @author     John Brand <john.brand@thyon.com> 
 * @package    Controller 
 */
class NewsletterExport extends Backend
{


	/**
	 * Return a form to choose a CSV file and import it
	 * @param object
	 * @return string
	 */
	public function exportRecipients(DataContainer $dc)
	{
		if ($this->Input->get('key') != 'export')
		{
			return '';
		}
		
		// get records
		$arrExport = array();
		$objRow = $this->Database->prepare("SELECT email,active FROM tl_newsletter_recipients WHERE pid=? ORDER by  email")
					->execute($dc->id);

		while ($objRow->next())
		{
			$arrExport[] = $objRow->row();			
		}

		// start output
		$exportFile =  'export_newsletter_recipients_' . date("Ymd-Hi");
		
		header('Content-Type: application/csv');
		header('Content-Transfer-Encoding: binary');
		header('Content-Disposition: attachment; filename="' . $exportFile .'.csv"');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Expires: 0');

		$output = '';
		$output .= '"E-mail","Active"'. "\n" ;

		foreach ($arrExport as $export) 
		{
			$output .= '"' . join('","', $export).'"' . "\n";
		}

		echo $output;
		exit;

	}


}

?>